var can1,can2;
var context1,context2; 
var lastTime;//上一帧的时间
var deltaTime;//两帧间隔的时间差
var bgpic=new Image();
var canWidth,canHeight;
var ane;
var fruit;
var mom;
var mx;
var my;
var baby;
var babyTails=[];
var babyEyes=[];
var babyBodys=[];
var momTails=[];
var momEyes=[];
var data;
var momBodyOranges=[];
var momBodyBlues=[];
var wave;//碰撞果实产生的圆圈
var halo;//喂小鱼产生的圆圈

var dust;//漂浮物
dustPic=[];
document.body.onload=game;

function game () {//游戏入口
	init();
	lastTime=Date.now();//获得当前时间
	deltaTime=0;
	gameloop();
}

function init () {//游戏初始化
	 can1=document.getElementById("canvas1");
	 context1=can1.getContext("2d");
	 can2=document.getElementById("canvas2");
	 context2=can2.getContext('2d');
	 
	 can1.addEventListener("mousemove",onmousemove,false);
	 
	  ane=new aneObj();//定义海葵对象
    
     bgpic.src="img/background.jpg"
     
     canWidth=can1.width;//获取canvas1的宽和高
     canHeight=can1.height;
     ane.init();//海葵初始化函数
     
     fruit=new fruitObject();//定义果实对象
     fruit.init();//果实初始化
     
     mom=new momObject();
     mom.init();
     
     mx=canWidth*0.5;
     my=canHeight*0.5;
     
     baby=new babyObject();
     baby.init();
     
     //小鱼尾巴摆动循环图片
     for (i=0;i<8;i++) {
     	
     	babyTails[i]=new Image();
     	babyTails[i].src="img/babyTail"+i+".png";
     }
     //小鱼眼睛睁眨图片循环
     for (i=0;i<2;i++) {
     	
     	babyEyes[i]=new Image();
     	babyEyes[i].src="img/babyEye"+i+".png";
     }
     //小鱼身体变白图片循环
     for (i=0;i<20;i++) {
     	babyBodys[i]=new Image();
     	babyBodys[i].src="img/babyFade"+i+".png";
     }
     //大鱼摇尾巴图片循环
     for (i=0;i<8;i++) {
     	momTails[i]=new Image();
     	momTails[i].src="img/bigTail"+i+".png";
     }
     //大鱼眨眼循环图片
     for (i=0;i<2;i++) {
     	momEyes[i]=new Image();
     	momEyes[i].src="img/babyEye"+i+".png";
     }
     data=new dataObject();
     //大鱼身体循环图片
     for (i=0;i<8;i++) {
     	momBodyOranges[i]=new Image();
     	momBodyBlues[i]=new Image();
     	momBodyOranges[i].src="img/bigSwim"+i+".png";
		momBodyBlues[i].src="img/bigSwimBlue"+i+".png";
     }
     context1.font="30px verdana";
	 context1.textAlign="center";
	 
	 wave=new waveObject();
	 wave.init();
     
     halo=new haloObject();
     halo.init();
     
     for (i=0;i<7;i++) {
     	dustPic[i]=new Image();
     	dustPic[i].src="img/dust"+i+".png";
     }
     dust=new dustObject();
     dust.init();
}
function gameloop () {
	window.requestAnimationFrame(gameloop);
	var now=Date.now();
	deltaTime=now-lastTime;
	lastTime=now;
	if(deltaTime>40){
		deltaTime=40;
	}
	drawbackground();
	ane.draw();//海葵绘制函数
	fruitMonitor();
	fruit.draw();//绘制果实
	
	context1.clearRect(0,0,canWidth,canHeight);//绘制大鱼之前先清除
	mom.draw();//绘制大鱼
	baby.draw();//绘制小鱼
	momFruitcollision();//检测大鱼碰撞果实消失
	momBabyCollision();//检测大鱼小鱼碰撞
	data.draw();
	
	wave.draw();//绘制大鱼吃果实圆圈
	halo.draw();//绘制大鱼喂小鱼圆圈
	
	dust.draw();//绘制漂浮物
	
}
function onmousemove (e) {
	if(!data.gameOver){
			if(e.offsetX||e.layerX){
				mx=e.offsetX == undefined ?e.layerX:e.offsetX;
				my=e.offsetY == undefined ?e.layerY:e.offsetY;
				console.log(mx);
			}
	}
	
	
}
